package com.hycreate.platform.common.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



import net.sf.json.JSONObject;


public class DateDayUtil {
	/**
	 * Java根据传入的指定日期以及增加的工作日天数计算出对应的日期
	 * 传入日期格式为yyyy-MM-dd
	 * @param date
	 * @param day
	 * @return
	 * @throws ParseException 
	 * 
	 */
	public static String request( String httpArg) {
        String httpUrl="http://www.easybots.cn/api/holiday.php";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?d=" + httpArg+"&ak=p364.f9e88b51f0e5078b5f8daccd536e760b";

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
	public String DateUtil(String date,int day) throws  java.text.ParseException{
		
			 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			 Date da=formatter.parse(date);
			 Date de = null;
			 for(int i=1;i<day+1;i++){
				 	Calendar calendar = Calendar.getInstance();
			        calendar.setTime(da);
			        calendar.add(Calendar.DAY_OF_MONTH, i);//+i今天的时间加一天
			        de = calendar.getTime();
			        SimpleDateFormat f=new SimpleDateFormat("yyyyMMdd");
			        String httpArg=f.format(de);
			        String jsonResult = request(httpArg);
			        JSONObject jsStr = JSONObject.fromObject(jsonResult);
			    if(Integer.parseInt(jsStr.getString(httpArg))!=0){
			    	day=(day+1);
			    }
			 }
			 String dateString=formatter.format(de);
			 return dateString;
		}

		public static void main(String[] args) throws  java.text.ParseException{
			DateDayUtil de=new DateDayUtil();
			String  data=de.DateUtil("2017-03-12",7);
			System.out.println(data);
		}
}
